package com.aixin.project.servlet.gensee;

import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.util.AxUtil;
import com.aixin.project.util.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Sufeng
 * Date: 12-5-16
 * Time: 下午10:02
 * To change this template use File | Settings | File Templates.
 */
public class GenseeValidateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String k = RequestUtil.getStr(request,"k",null);
        if(k!=null){
            //解密，比对memcached
            String decodeK = AxUtil.base64Decode(k);
            String[] karr = decodeK.split("_");
            if(karr==null || karr.length!=3)
            {
                out.write("fail");
            }else{
                long uid = Long.valueOf(karr[0]);
                long courseId = Long.valueOf(karr[1]);
                int pointCount = CourseOrderHandler.pointCount(uid,courseId);
                if(pointCount<=0)
                    out.write("fail");
                else
                    out.write("pass");
            }
        }else{
            out.write("fail");
        }
        out.close();
    }

}
